package xj.util.gui;

import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;

public class HTMLFrame extends JFrame {

  public static final String FORWARD = "Forward"; 
  public static final String BACK    = "Back"; 

  public HTMLFrame(String title) {
    this(title, null, false);
  }

  public HTMLFrame(String title, 
		   AbstractButton[] comps) {
    this(title, comps, false);
  }

  public HTMLFrame(String title, 
		   AbstractButton[] comps, 
		   boolean hasIndex) {
    this(title, comps, hasIndex, 600, 400);
  }

  public HTMLFrame(String title, 
		   AbstractButton[] comps, 
		   boolean hasIndex,
		   int width,
		   int height) {
    super(title);
    setLayout(new BorderLayout());
    JPanel bpanel = new JPanel();
    bpanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    if (comps != null) {
      int n = comps.length;
      for (int i = 0; i < n; i++) {
	if (comps[i] != null) {
	  bpanel.add(comps[i]);
	  String cmd = comps[i].getActionCommand();
	  if (FORWARD.equals(cmd)) { 
	    forwardButton = comps[i];
	  } else if (BACK.equals(cmd)) { 
	    backButton = comps[i];
	  }
	}
      }
      bpanel.add(Box.createHorizontalStrut(6));
    }
    JButton button = new JButton("Close");
    bpanel.add(button);
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent a) {
	close();
      }
    });

    html = new HTMLPane(this);
    if (hasIndex) { 
      index = new HTMLPane(this, HTMLPane.createHyperLinkListener(html)); 
      JSplitPane split = new JSplitPane();
      split.setLeftComponent(index);
      split.setRightComponent(html);
      split.setDividerSize(4);
      int loc = width / 3; 
      if (loc < 180) { 
	loc = 180; 
      }
      split.setDividerLocation(loc);      
      add(split, BorderLayout.CENTER);
    } else { 
      add(html, BorderLayout.CENTER);
    }

    add(bpanel, BorderLayout.SOUTH);
    updateButtonState();

    setAlwaysOnTop(true);
    setDefaultCloseOperation(HIDE_ON_CLOSE);
    setSize(width, height);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation(screenSize.width / 2 - 300, screenSize.height / 2 - 200);
  }

  public void setResourceURL(String resourceURL) {
    html.setResourceURL(resourceURL);
  }

  public void setURL(String url) {
    html.setURL(url);
  }

  public void setURL(URL url) {
    html.setURL(url);
  }

  public void scrollToReference(String reference) {
    html.scrollToReference(reference);
  }

  public void setText(String text) {
    html.setText(text);
  }

  public void setHTMLFont(Font font) { 
    if (font != null) { 
      if (html != null)
	html.setFont(font);
      if (index != null)
	index.setFont(font);
    }
  }

  public void setIndexResourceURL(String resourceURL) {
    if (index != null) { 
      index.setResourceURL(resourceURL);
    }
  }

  public void setIndexURL(String url) {
    if (index != null) { 
      index.setURL(url);
    }
  }

  public void doCommand(String command) { 
    if (command != null) { 
      if (FORWARD.equals(command)) { 
	forward();
      } else if (BACK.equals(command)) { 
	back();
      }
    }
  }

  public void forward() {
    html.forward();
  }

  public void back() { 
    html.back();
  }

  public void close() {
    setVisible(false);
  }

  protected void updateButtonState() {
    if (forwardButton != null) {
      forwardButton.setEnabled(html.canForward());
    }
    if (backButton != null) { 
      backButton.setEnabled(html.canBack());
    }
  } 

  protected HTMLPane html = null;
  protected HTMLPane index = null;

  protected AbstractButton forwardButton = null;
  protected AbstractButton backButton = null;

}
